package com.scc.aop;

import com.scc.mapper.EmpOperationLogMapper;
import com.scc.pojo.EmpOperationLog;
import com.scc.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;


@Aspect
@Slf4j
@Component
public class EmpLogAspect {
    ///获取http
    @Autowired
    private HttpServletRequest request;

    ///注入数据层接口
    @Autowired
    private EmpOperationLogMapper empOperationLogMapper;

    @Pointcut("@annotation(com.scc.aop.EmpLog)")
    public void empLogPointCut() {}

    @Around("empLogPointCut()")
    public Object empLog(ProceedingJoinPoint joinPoint) throws Throwable {

        //获取JWT令牌
        String jwt = request.getHeader("token");

        //解析jwt获取员工账号，姓名
        Map<String, Object> claims = JwtUtil.parseJwt(jwt);
        String account = (String) claims.get("account");
        String name =  (String) claims.get("name");

        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = now.format(formatter);
        LocalDateTime operationTime = LocalDateTime.parse(time, formatter);

        //获取方法的名字--》推断出用户的操作类型
        String methodName = joinPoint.getSignature().getName();

        String operationType = null;
        /*
          1、修改照片
          2、修改密码操作
          3、考勤操作
          4、请假操作
          5、离职操作
          6、查看个人信息操作
          7、培训活动操作
          8、团建活动操作
          */
        ///根据方法名来推断用户操作了什么
        if(methodName.contains("upload")){
            operationType = "修改头像";
        }else if(methodName.contains("Attendance")){
            operationType = "考勤操作";
        }else if(methodName.contains("Leave")){
            operationType = "请假操作";
        }else if(methodName.contains("Resign")){
            operationType = "离职操作";
        }else if(methodName.contains("TrainingActivity")){
            operationType = "培训活动操作";
        }else if(methodName.contains("TeamBuildingActivity")||methodName.contains("TeamBuildingActivities")){
            operationType = "团建活动操作";
        }else if(methodName.contains("modifyPassword")){
            operationType = "修改密码";
        }else if(methodName.contains("empApply")){
            operationType = "修改个人信息";
        }else if(methodName.contains("getSalaryMsg")){
            operationType = "员工查看工资操作";
        }else if(methodName.contains("friend")){
            operationType = "聊天";
        }

        //获取员工日志对象
        EmpOperationLog empOperationLog = new EmpOperationLog();
        empOperationLog.setAccount(account);
        empOperationLog.setName(name);
        empOperationLog.setOperationTime(operationTime);
        empOperationLog.setOperationType(operationType);


        //新增员工日志
        empOperationLogMapper.insertEmpOperationLog(empOperationLog);

        //执行原始方法
        return joinPoint.proceed();
    }
}
